package com.tuyan.aixiang.component;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tuyan.aixiang.R;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;

public class VidControl extends RelativeLayout {

    @BindView(R.id.iv_play_ctl)
    ImageView ivPlayCtl;

    @BindView(R.id.iv_voice_ctl)
    ImageView ivVoiceCtl;

    @BindView(R.id.iv_hd_ctl)
    ImageView ivHdCtl;

    @BindView(R.id.iv_refresh)
    ImageView ivRefresh;

    @BindView(R.id.iv_fullscreen)
    ImageView ivFullscreen;

    @BindView(R.id.ll_vid_control)
    LinearLayout llVidControl;

    @BindView(R.id.iv_big_play)
    ImageView ivBigPlay;

    @BindView(R.id.iv_back)
    ImageView ivBack;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.ll_full_title)
    LinearLayout llFullTitle;

    @BindView(R.id.tv_split)
    TextView tvSplit;

    @BindView(R.id.ll_full_ctl)
    LinearLayout llFullCtl;

    @BindView(R.id.iv_ptz_ctl)
    ImageView ivPtz;

    @BindView(R.id.iv_takl_ctl)
    ImageView ivTalk;

    boolean play = true;

    boolean hd = false;

    boolean fullscreen = false;

    boolean mute = false;

    boolean showControl = true;

    boolean ptz = false;

    boolean talk = false;


    private ControlAction controlAction;

    public VidControl(Context context) {
        this(context, null);
    }

    public VidControl(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VidControl(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = LayoutInflater.from(context).inflate(R.layout.vid_control, this);
        ButterKnife.bind(contentView, this);
        init();
    }

    public void setup(ControlAction action) {
        this.controlAction = action;
    }

    public interface ControlAction {
        void onTogglePlay(boolean play);

        void onToggleVoice(boolean mute);

        void onToggleFullscreen(boolean full);

        void onRefresh();

        void onToggleResolution(boolean hd);

        void onTogglePtz(boolean on);

        void onToggleTalk(boolean on);
    }

    public void syncPlay(boolean play) {
        this.play = play;
        this.updateView();
    }

    public void syncResolution(boolean hd) {
        this.hd = hd;
        this.updateView();
    }

    public void syncVoice(boolean mute) {
        this.mute = mute;
        this.updateView();
    }

    @SuppressLint("ClickableViewAccessibility")
    public void init() {
        ivPlayCtl.setOnClickListener(v -> togglePlay());

        ivVoiceCtl.setOnClickListener(v -> toggleVoice());

        ivFullscreen.setOnClickListener(v -> toggleFullscreen());

        ivHdCtl.setOnClickListener(v -> toggleResolution());

        ivRefresh.setOnClickListener(v -> doRefresh());

        ivBigPlay.setOnClickListener(v -> togglePlay());

        ivPtz.setOnClickListener(v -> togglePtz());

//        ivTalk.setOnClickListener(v -> toggleTalk());

        ivTalk.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                toggleTalk(true);
            }
            if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                toggleTalk(false);
            }
            return true;
        });

        setOnClickListener(v -> switchControlVisibility());

        ivBack.setOnClickListener(v -> {
            toggleFullscreen();
        });

    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }


    private void togglePlay() {
        this.play = !this.play;
        controlAction.onTogglePlay(this.play);
        updateView();
    }

    private void toggleVoice() {
        this.mute = !this.mute;
        controlAction.onToggleVoice(this.mute);
        updateView();
    }

    private void togglePtz() {
        this.ptz = !this.ptz;
        controlAction.onTogglePtz(this.ptz);
        updateView();
    }


    private void toggleTalk(boolean talking) {
        this.talk = talking;
        controlAction.onToggleTalk(this.talk);
        updateView();
    }

    public void syncTalk(boolean on) {
        this.talk = on;
        updateView();
    }

    public void syncPtz(boolean on) {
        this.ptz = on;
        updateView();
    }


    public void triggerFullscreen(boolean full) {
        this.fullscreen = full;
        controlAction.onToggleFullscreen(full);
        updateView();
    }

    private void toggleFullscreen() {
        this.fullscreen = !this.fullscreen;
        controlAction.onToggleFullscreen(this.fullscreen);
        switchControlVisibility();
    }

    private void toggleResolution() {
        this.hd = !this.hd;
        controlAction.onToggleResolution(this.hd);
        updateView();
    }

    private void doRefresh() {
        controlAction.onRefresh();
    }

    private void updateView() {
        ivPlayCtl.setImageResource(
                this.play ? R.drawable.icon_vid_pause : R.drawable.icon_vid_play
        );

        if (this.play) {
            ivBigPlay.setVisibility(GONE);
        } else {
            ivBigPlay.setVisibility(VISIBLE);
        }

        ivVoiceCtl.setImageResource(
                this.mute ? R.drawable.icon_vid_voice_off : R.drawable.icon_vid_voice_on
        );

        ivHdCtl.setImageResource(
                this.hd ? R.drawable.icon_vid_hd_on : R.drawable.icon_vid_hd
        );

        ivPtz.setImageResource(
                this.ptz ? R.drawable.icon_ptz_on : R.drawable.icon_ptz_off
        );

        ivTalk.setImageResource(
                this.talk ? R.drawable.icon_talk_on : R.drawable.icon_talk_off
        );

        updateFullctl();
    }

    private void switchControlVisibility() {
        this.showControl = !this.showControl;
        if (this.showControl) {
            if (fullscreen) {
                llFullTitle.setVisibility(VISIBLE);
            }
            llVidControl.setVisibility(View.VISIBLE);
        } else {
            llVidControl.setVisibility(View.INVISIBLE);
            llFullTitle.setVisibility(INVISIBLE);
        }
        updateFullctl();
    }

    private void updateFullctl() {
        if (this.fullscreen) {
            llFullCtl.setVisibility(VISIBLE);
            tvSplit.setVisibility(VISIBLE);
        } else {
            llFullTitle.setVisibility(INVISIBLE);
            tvSplit.setVisibility(GONE);
            llFullCtl.setVisibility(GONE);
        }
    }

    public boolean isFullscreen() {
        return this.fullscreen;
    }

}
